<script>
export default {
  name: "RoleList",
  computed: {
    /*stateBoolean(val) {
      return val === 1;
    }*/
  },
  data() {
    return {
      queryInfo: {
        keyword: '',
        currentPage: 1,
        pageSize: 8
      },
      total: 0,
      roles: [],
      listLoading: false,
      sels: [],
      editFormVisible: false,

      editForm: {
        id: '',
        name: '',
        sn: '',
        permissionSnList: [],
      },
      editLoading: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permissionTree: [],
      rolePermissionTree:[],
      rolePermissionIds: [],
      rolePermissionNodes: [],
    }
  },
  methods: {
    getRole() {
      this.$http.post("/role", this.queryInfo).then(res => {
        if (res.data.success) {
          this.roles = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.$message({
            message: res.data.msg,
            type: "error",
            showClose: true
          })
        }
      }).catch(err => {
        this.$message({
          message: "请求异常",
          type: "error",
          showClose: true
        })
      }).finally(() => {
        this.listLoading = false;
      })
    },
    handleAdd() {
      this.editFormVisible = true;
      this.editForm = {
        id: '',
        name: '',
        sn: '',
        permissionSnList: [],
      };
      this.rolePermissionIds = [];
      this.queryPermissionTree();
    },
    handleSizeChange(val) {
      this.queryInfo.pageSize = val;
      this.getRole();
    },
    selsChange(sels) {
      this.sels = sels
    },
    batchRemove() {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        let ids = this.sels.map(item => item.id);
        // console.log(ids)
        this.$http.patch("/role", ids).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getRole();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    handleCurrentChange(val) {
      this.queryInfo.currentPage = val;
      this.getRole();
    },
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.queryPermissionTree();
      // this.queryCurrentPermission(row.id);
      this.queryCurrentPermissionIds(row.id);
      /*setTimeout(() => {
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys(this.rolePermissionIds);
        })
      },500);*/
      this.editForm = Object.assign({}, row);
    },
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/role/" + row.id).then(res => {
          if (res.data.success) {
            this.$message({
              message: res.data.msg,
              type: "success",
              showClose: true
            })
            this.getRole();
          }
        }).catch(err => {
          this.$message({
            message: "请求异常",
            type: "error",
            showClose: true
          })
        }).finally(() => {
          this.listLoading = false;
        })
      }).catch(() => {

      });
    },
    editSubmit() {
      // 获取对象数组的sn属性
      this.editForm.permissionSnList = this.$refs.tree.getCheckedNodes().map(item => item.sn);
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.editLoading = true;
          this.$http.put("/role", this.editForm).then(res => {
            if (res.data.success) {
              this.editFormVisible = false;
              this.$message({
                message: res.data.msg,
                type: "success",
                showClose: true
              })
              this.getRole();
            }
          }).catch(err => {
            this.$message({
              message: "请求异常",
              type: "error",
              showClose: true
            })
          }).finally(() => {
            this.editLoading = false;
          })
        }
      })
    },
    queryPermissionTree() {
      this.$http.get("/permission/tree").then(res => {
        this.permissionTree = res.data.data;
      }).catch(error => {
        this.$message({
          message: "权限树获取失败",
          type: "error",
          showClose: true
        })
      })
    },
    queryCurrentPermission(id) {
      this.$http.get("/permission/tree/" + id).then(res => {
        this.rolePermissionTree = res.data.data;
      }).catch(error => {
        this.$message({
          message: "角色权限树获取失败",
          type: "error",
          showClose: false
        })
      })
    },
    queryCurrentPermissionIds(id) {
      this.$http.get("/permission/ids/" + id).then(res => {
        this.rolePermissionIds = res.data.data;
      }).catch(error => {
        this.$message({
          message: "角色权限树获取失败",
          type: "error",
          showClose: false
        })
      })
    },
  },
  mounted() {
    this.getRole()
  }
}
</script>

<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="queryInfo.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getRole">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="roles" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              max-height="460px"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="角色名称" width="200" sortable/>
      <el-table-column prop="sn" label="唯一标识" width="200" sortable/>
      <el-table-column label="操作" width="240">
        <template v-slot="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.currentPage"
          :page-sizes="[3, 5, 8, 10, 20]"
          :page-size="8"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editFormVisible">
<!--      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">-->
      <el-form :model="editForm" label-width="80px" ref="editForm">
        <el-form-item label="角色名称">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="唯一标识" prop="intro">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="拥有权限" prop="intro">
          <div class="dialogDiv">
            <el-tree
                :data="permissionTree"
                show-checkbox
                node-key="id"
                ref="tree"
                :default-checked-keys="rolePermissionIds"
                highlight-current
                accordion
                :props="defaultProps">
            </el-tree>
          </div>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button></div>
    </el-dialog>
  </section>
</template>

<style scoped>
.dialogDiv {
  height: 200px;
  overflow: auto;
}
</style>
